
#include <QDebug>
#include <QMouseEvent>
#include <QSharedPointer>
#include <QTimer>
#include <QPainter>
#include <QStyleOption>
#include <QPainter>

#include "ViewStyle.h"
#include "widget_enhanced.h"

owid::Widget_Enhanced::Widget_Enhanced(QWidget *parent)
    : QWidget(parent),
      enhanced_switch(Enhanced_Switch::None),
      _power_hint_durationClick(2000),
      _power_hint_string(QString("请补充提示信息")),
      _power_hint_timer(QSharedPointer<QTimer>(new QTimer)),
      hint_wid(QSharedPointer<Label_Show_Hint> (new Label_Show_Hint()))
{
    // 初始化时建立connect链接
    this_connect();

}

void owid::Widget_Enhanced::POWER_HINT_timer(size_t o_how_time_show)
{
    this->_power_hint_durationClick = o_how_time_show;
}

void owid::Widget_Enhanced::POWER_HINT_msg(const QString &str, const QFont *font)
{
    // 开启 POWER_HINT功能
    this->enhanced_on(Enhanced_Switch::POWER_HINT);
    if(font == nullptr){
        font = &(this->font());
    }

    this->hint_wid->set_HintLab_txt(str,*font);
}

void owid::Widget_Enhanced::POWER_DRAW_SHADOW_style(int width, short intensity, bool top, bool right, bool down, bool left)
{
    // 设置阴影绘制参数
    this -> pds_width      = width;
    this -> pds_intensity  = intensity;

    this -> pds_top        = top;
    this -> pds_right      = right;
    this -> pds_down       = down;
    this -> pds_left       = left;

    // 步长
    this->pds_step_size = (255 - pds_intensity) / pds_width;


    // 开启阴影功能
    if(!enhanced_open_Or_not(Enhanced_Switch::POWER_DRAW_SHADOW)){
        enhanced_on(Enhanced_Switch::POWER_DRAW_SHADOW);
    }

}


void owid::Widget_Enhanced::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    emit release();


    QWidget::mouseReleaseEvent(event);
}

void owid::Widget_Enhanced::paintEvent(QPaintEvent *event)
{

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    QWidget::style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    // 阴影绘制
    if(enhanced_open_Or_not(Enhanced_Switch::POWER_DRAW_SHADOW)){
        draw_shadow();
    }
    qDebug() << "draw Widget_Enhanced";

    QWidget::paintEvent(event);
}

void owid::Widget_Enhanced::now_press()
{
    // POWER_HINT功能逻辑代码
    if(this->enhanced_open_Or_not(Enhanced_Switch::POWER_HINT) == true){
        _power_hint_timer->start(_power_hint_durationClick);
    }

}

void owid::Widget_Enhanced::now_release()
{
    // POWER_HINT功能逻辑代码
    if(this->enhanced_open_Or_not(Enhanced_Switch::POWER_HINT) == true){
        _power_hint_timer->stop();
        this->hint_wid->hide();
    }


}

void owid::Widget_Enhanced::POWER_HINT_timer_trigger()
{

    const QPoint point =  this->mapToGlobal(QPoint(this->width(),this->height()));
    hint_wid ->show_HintLab(point);

}

void owid::Widget_Enhanced::draw_shadow()
{
    // 绘制阴影
    // 1. 初始化一个QPainter对象用来绘制
    // 2. QPainter::Antialiasing，让引擎尽可能的消除锯齿
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor color;

    // 上边框
    QLine line_top_border = QLine(0, 0, this->width() - (this->pds_width), 0);
    // 左边框
    QLine line_left_border = QLine(0, 0, 0, this->height()-(this->pds_width) );

    int color_ladder = pds_intensity;
    color.setRgb(color_ladder,color_ladder,color_ladder);
    painter.setPen(color);

    painter.drawLine(line_top_border);
    painter.drawLine(line_left_border);

    // 此处循环次数为阴影宽度
    for(size_t i=0; i < this->pds_width; i++)
    {

        // 右边阴影
        QLine line_right_border = QLine(this->width() - (this->pds_width - i),
                                        0+i,
                                        this->width() - (this->pds_width - i),
                                        this->height()-(this->pds_width-i)
                                        );
        // 下边阴影
        QLine line_bottom_border = QLine(0+i,
                                        this->height() - (this->pds_width - i),
                                        this->width() - (this->pds_width - i),
                                        this->height()-(this->pds_width-i)
                                        );

        color_ladder = pds_intensity + pds_step_size*i;
        color.setRgb(color_ladder,color_ladder,color_ladder);
        painter.setPen(color);


        // 7. 绘制线路
        // painter.drawRect(rect);
        painter.drawLine(line_right_border);
        painter.drawLine(line_bottom_border);

    }
}

void owid::Widget_Enhanced::enhanced_on(owid::Widget_Enhanced::Enhanced_Switch o_enum)
{
    // 或增之术
    this->enhanced_switch |= o_enum;
}

void owid::Widget_Enhanced::enhanced_off(owid::Widget_Enhanced::Enhanced_Switch o_enum)
{
    // 异或消减之术
    this->enhanced_switch ^= o_enum;
}


void owid::Widget_Enhanced::this_connect()
{
    // 当一次点击信号触发窗口会做些什么？
    connect(this, &Widget_Enhanced::press,
            this, &Widget_Enhanced::now_press);

    // 当一次点击的释放触发信号时，窗口做写什么？
    connect(this, &Widget_Enhanced::release,
            this, &Widget_Enhanced::now_release);

    // 当增强功能 POWER_HINT 的定时器触发时，系统会做些什么？
    connect(this->_power_hint_timer.data(), &QTimer::timeout,
            this, &Widget_Enhanced::POWER_HINT_timer_trigger);
}

bool owid::Widget_Enhanced::enhanced_open_Or_not(owid::Widget_Enhanced::Enhanced_Switch o_enum)
{
    // 和查之术
    return (this->enhanced_switch & o_enum)  == o_enum;
}

void owid::Widget_Enhanced::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    QWidget::mousePressEvent(event);
}
